﻿using DBUtil.FastEntity.Annotations;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Collections.Generic;
using System.Text;

namespace DBUtil.Orm.Test
{

    [TestClass]
    public class TestBase
    {
        protected DBAccess db = null;
        public TestBase()
        {
            DBUtil.DBFactory.AddDBSupport<DBUtil.Provider.SqlServer.SqlServerDBFactory>();
            //sqlserver 2014
            db = DBFactory.CreateDB("SQLSERVER", "Data Source=.;Initial Catalog=test;User ID=sa;Password=123456;Pooling=True;Max Pool Size=20000;");
        }

        /// <summary>
        /// 重置Person表
        /// </summary>
        public void ResetPersonTable()
        {
            db.Manage.DropTableIfExist("t_person");
            db.ExecuteSql(@"create table t_person(
    id int primary key,
    age int,
    Name varchar(50),
    Addr varchar(200),
    Birth datetime,
    Sex int,
    Color varchar(50)
)
");
            //插入数据
            db.InsertBatch("t_person", new Person()
            {
                Id = 1,
                Name = "小明",
                Age = 18,
                Addr = "天明路",
                Birth = DateTime.Parse("1995-01-02"),
                Color = "green",
                Sex = Sex.Male
            },
            new Person()
            {
                Id = 2,
                Name = "小红",
                Age = 19,
                Addr = "文化路",
                Birth = DateTime.Parse("1994-01-02"),
                Color = "green",
                Sex = Sex.FeMale
            },
            new Person()
            {
                Id = 3,
                Name = "张三",
                Age = 22,
                Addr = "文化路",
                Color = "green",
                Sex = Sex.None
            });
        }

        #region 简单的单表实体
        /// <summary>
        /// 人员表
        /// </summary>
        [MapTable(Name = "dbo.t_person")]
        public class Person
        {
            /// <summary>
            /// 主键
            /// </summary>
            [MapKey]
            public int Id { get; set; }

            /// <summary>
            /// 年龄
            /// </summary>
            public int? Age { get; set; }

            /// <summary>
            /// 姓名
            /// </summary>
            [MapColumn]
            public string Name { get; set; }

            /// <summary>
            /// 住址
            /// </summary>
            public string Addr { get; set; }

            /// <summary>
            /// 生日
            /// </summary>
            public DateTime? Birth { get; set; }

            /// <summary>
            /// 性别
            /// </summary>
            public Sex Sex { set; get; }

            /// <summary>
            /// 健康码
            /// </summary>
            public string Color { get; set; }
        }

        /// <summary>
        /// 性别
        /// </summary>
        public enum Sex
        {
            /// <summary>
            /// 未知
            /// </summary>
            None,
            /// <summary>
            /// 男
            /// </summary>
            Male,
            /// <summary>
            /// 女
            /// </summary>
            FeMale
        }
        #endregion
    }
}
